!// Dmitry.Konovalov@jcu.edu.au Dmitry.A.Konovalov@gmail.com, 2013

module jm_main_mod;
use dbg_mod;
use jm_data_mod;
use jm_e1_mod;
use jm_yk_mod;
use qdr_mod;
use lgrr_mod, only: lgrr_lcr3;
implicit none;
private;
public jm_main_run;

contains;
SUBROUTINE jm_main_run(); 
  character(LEN=64) :: name = "jm_main_run"; 
  call dbg_in(name); 
  
  call jm_data_setup();
  !todo: print JM_DATA for testing
  
  select case (JM_DATA%CALC_TYPE)
    case (JM_DATA_CALC_HY)
       call jm_main_calc_hy(JM_DATA); 
    !case (ID_JMCOEF)
    !  call test_jmcoef();
    case default
       stop
  end select
  
  call dbg_out(name); 
end SUBROUTINE


SUBROUTINE jm_main_calc_hy(pd);
  real(DP) :: orthErr;
  character(LEN=64) :: name = "jm_main_calc_hy"; 
  type(JM_DATA_T) :: pd; ! pd-pointer to data
  type(E1_CONF_OPT_T) :: optE1; 
  call dbg_in(name); 
   
  call grid_lcr(pd%lcr);  !call vec_dbg("lcr", xgrid);
  call qdr_lcr(pd%lcr);
  
  ! testing that the lcr grid is accurate for 1s,2s,2p of Hydrogen and He^{++}, and Li^{2+}
  call e1_hlike_n2_assert_lcr(D1, pd%lcr, EPS13, EPS12);
  call e1_hlike_n2_assert_lcr(D2, pd%lcr, EPS13, EPS12);
  call e1_hlike_n2_assert_lcr(D3, pd%lcr, EPS13, EPS12);
  call yk_assert_lcr(pd%lcr, EPS13, EPS12, EPS12); ! maxErr
  
  pd%orthN => lgrr_lcr3(pd%lcr, pd%jmaxL, pd%jmaxN, pd%jmLmbd);
  
  orthErr = qdr_lcr_max_orth_err3(pd%lcr, pd%orthN);
  call dbg_dp("orthErr=", orthErr);

  call loadTrgtOptE1(optE1, pd);
  pd%tConfArr => e1_conf(optE1);
  
  call e1_conf_h(pd%tConfH, pd%tConfArr);

  call dbg_out(name); 
end SUBROUTINE

SUBROUTINE loadTrgtOptE1(opt, pd); 
  character(LEN=64) :: name = "loadTrgtOptE1"; 
  type(JM_DATA_T) :: pd; ! pd-pointer to data
  type(E1_CONF_OPT_T) :: opt; 
  call dbg_in(name); 
  opt%maxL = pd%tmaxL;
  opt%maxN => pd%tmaxN;
end SUBROUTINE
  
end module